<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(67877605@qq.com)
// +----------------------------------------------------------------------

// 导航
class FriendLinkAction extends CommonAction
{
	//列表
	public function index() 
	{
		$sql_str = 'SELECT * FROM '.C("DB_PREFIX").'friend_link  ';

		$model = M();
    $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);
    
		$this->display ();
		return;
	}
	
	public function add()
	{
		//导航分类
		$cateList = D("SiteCate")->getSiteCateList();
		$this->assign("cateList",$cateList);
		
		$citys = D("GroupCity")->where("status = 1")->order("sort asc,id desc,py asc")->findAll();
		$this->assign("citys",$citys);
		
		$new_sort = D("Site")->where("status=1") -> max("sort") + 1;
		$this->assign("new_sort",$new_sort);
		
		$ruleType = D("CollectRuleType")->where("status = 1")->order("sort asc,id desc")->findAll();
		$this->assign("ruleType",$ruleType);
		
		$this->display();
	}
	
	public function insert()
	{
		$model = D(MODULE_NAME);
		$_REQUEST['content'] = addslashes($_REQUEST['content']);
		if (false === $model->create())
		{
			$this->error($model->getError());
		}
		//保存当前数据对象
		$id = $model->add();
		if ($id !== false)
		{
			$upload_list = $this->uploadFile(1,"site");
			if($upload_list)
			{
				D("FriendLink")->where("id=$id")->setField("img",$upload_list[0]['recpath'].$upload_list[0]['savename']);
			}
			
			$data = array();
			$data['url'] = trim($_POST['url']);
			$data['sort'] = trim($_POST['sort']);
			$data['name'] = trim($_POST['name']);
			$data['width'] = trim($_POST['width']);
			$data['height'] = trim($_POST['height']);
			D("FriendLink")->save($data);
			
			$this->saveLog(1, $id);
			$this->success(L('ADD_SUCCESS'));
		}
		else
		{
			//失败提示
			$this->saveLog(0, $id);
			$this->error(L('ADD_FAILED'));
		}
	}
	
	public function edit()
	{
		$model = M(MODULE_NAME);
		
		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById($id);
		$this->assign('vo', $vo);
		
		$this->display();
	}
	
	public function update()
	{
		$model = D(MODULE_NAME);
		if (false === $model->create())
		{
			$this->error($model->getError());
		}
		
		// 更新数据
		$bln = $model->save();
		$id=intval($_REQUEST['id']); //友情链接ID
		if (false !== $bln)
		{
			$upload_list = $this->uploadFile(1,"link");
			if($upload_list)
			{
				$img = D("FriendLink")->where("id=$id")->getField("img");
				unlink($this->getRealPath().$img);
				D("FriendLink")->where("id=$id")->setField("img",$upload_list[0]['recpath'].$upload_list[0]['savename']);
			}
			
			$data = array();
			$data['url'] = trim($_POST['url']);
			$data['sort'] = trim($_POST['sort']);
			$data['name'] = trim($_POST['name']);
			$data['width'] = trim($_POST['width']);
			$data['height'] = trim($_POST['height']);
			D("FriendLink")->save($data);
			
			//成功提示
			$this->saveLog(1);
			$this->success(L('EDIT_SUCCESS'));
		}
		else
		{
			//错误提示
			$this->saveLog(0);
			$this->error(L('EDIT_FAILED'));
		}
	}
	
	public function foreverdelete ()
	{
		//删除指定记录
		$model = D(MODULE_NAME);
		if (!empty($model))
		{
			$pk = $model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id))
			{
				$condition = array($pk => array('in' , explode(',', $id)));
				if (false !== $model->where($condition)->delete())
				{
					D("SiteRules")->where($condition)->delete();
					$this->saveLog(1);
					$this->success(L('DEL_SUCCESS'));
				}
				else
				{
					$this->saveLog(0);
					$this->error(L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error(L('INVALID_OP'));
			}
		}
		$this->forward();
	}
	
	public function getSiteLinks($id)
	{
		$url = U('CollectSite/munual',array('id'=>$id));
		$urltest = "admin/services/index.php?act=test&id=$id";

		return "";
	}
	
	public function checkKey()
	{
		$key = $_REQUEST['key'];
		
		if(D(MODULE_NAME)->checkKey($key))
			echo 1;
		else
			echo 0;
	}
	
	//搜索站点
	public function getSites()
	{
		$sites = array();
		$key = trim($_REQUEST['key']);
		if(!empty($key))
		{
			$where['name'] = array("like","%$key%");
			$where['site_key'] = array("like","%$key%");
			$where['url'] = array("like","%$key%");
			$where['_logic'] = 'or';
			$condition['_complex'] = $where;
			
			$sites = D(MODULE_NAME)->where($condition)->field("id,name")->order("sort asc,id asc")->limit(30)->select();
		}
		else
		{
			$sites = D(MODULE_NAME)->where($condition)->field("id,name")->order("sort asc,id asc")->limit(30)->select();
		}
		
		echo json_encode($sites);
	}
	
	//修改推荐状态
	public function updateIsBest()
	{
		$id = intval($_REQUEST['id']);
		$isBest = D(MODULE_NAME)->where('id='.$id)->getField("is_best");
		
		if($isBest == 0)
			$isBest = 1;
		else
			$isBest = 0;
			
		if(D(MODULE_NAME)->where('id='.$id)->setField("is_best",$isBest))
			echo $isBest;
		else
			echo "err";
	}
	
	//修改推荐状态
	public function updateIsAuth()
	{
		$id = intval($_REQUEST['id']);
		$is_auth = D(MODULE_NAME)->where('id='.$id)->getField("is_auth");
		
		if($is_auth == 0)
			$is_auth = 1;
		else
			$is_auth = 0;
			
		if(D(MODULE_NAME)->where('id='.$id)->setField("is_auth",$is_auth))
			echo $is_auth;
		else
			echo "err";
	}
}
?>